package com.chefu.carowner.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chefu.carowner.pojo.UserReceiveAddress;
import com.chefu.carowner.service.UserReceiveAddressService;
import com.chefu.common.pojo.R;
import com.chefu.common.utils.auth.UserHolder;
import com.chefu.common.utils.auth.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/carOwner")
public class UserReceiveAddressController {

    @Autowired
    UserReceiveAddressService addressService;

    /**
     * 查询车主收货地址列表
     */
    @GetMapping("/address")
    public R findAddressList(){
        Long id = UserHolder.getUser().getId();
        List<UserReceiveAddress> addressList = addressService.list(new QueryWrapper<UserReceiveAddress>().eq("user_id", id));
        return R.ok().data("addressList",addressList);
    }

    /**
     * 根据id查询车主收货地址
     */
    @GetMapping("/address/{id}")
    public R findById(@PathVariable("id")Long id){
        UserReceiveAddress address = addressService.getById(id);
        return R.ok().data("address", address);
    }

    /**
     * 新增车主收货地址
     */
    @PostMapping("/address")
    public R addAddress(@RequestBody UserReceiveAddress address){
        UserInfo user = UserHolder.getUser();
        if (null != user){
            address.setUserId(user.getId());
        }
        addressService.save(address);
        return R.ok();
    }

    /**
     * 修改车主收货地址
     */
    @PutMapping("/address")
    public R updateAddress(@RequestBody UserReceiveAddress address){
        addressService.updateById(address);
        return R.ok();
    }
}

